home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #1 / Amiga Plus CD - 2000 - No. 1.iso / Tools / Dev / Meshwriter / raw.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-12-03  |  6.2 KB  |  200 lines

  1. /*
  2. **      $VER: raw.c 1.00 (13.02.1999)
  3. **
  4. **      Creation date : 19.11.1998
  5. **
  6. **      Description       :
  7. **         Standart saver module for meshwriter.library.
  8. **         Saves the mesh as RAW file.
  9. **
  10. **
  11. **      Written by Stephan Bielmann
  12. **
  13. */
  14.  
  15. /*************************** Includes *******************************/
  16.  
  17. /*
  18. ** Amiga includes
  19. */
  20. #include <dos/dos.h>
  21. #include <dos/stdio.h>
  22.  
  23. #include <clib/dos_protos.h>
  24. #include <clib/alib_stdio_protos.h>
  25.  
  26. /*
  27. ** Project includes
  28. */
  29. #include "meshwriter_private.h"
  30. #include "utilities.h"
  31.  
  32. /**************************** Defines *******************************/
  33.  
  34. /*
  35. ** Number of elements in the buffers
  36. */
  37. #define Ci_BUFFERS 100    // constant size of all used buffers in this mudule
  38.  
  39. /*********************** Type definitions ***************************/
  40.  
  41. /*
  42. ** Private type definitions
  43. */
  44. typedef struct {
  45.     FLOAT x1,y1,z1;
  46.     FLOAT x2,y2,z2;
  47.     FLOAT x3,y3,z3;
  48. } RAWTriangle;
  49.  
  50. /********************** Private functions ***************************/
  51.  
  52. /********************** Public functions ****************************/
  53.  
  54. /********************************************************************\
  55. *                                                                    *
  56. * Name         : write3RAWA                                          *
  57. *                                                                    *
  58. * Description  : Writes a standart RAW ascii file.                   *
  59. *                                                                    *
  60. * Arguments    : rawfile IN  : An already opened file stream.        *
  61. *                mesh    IN  : Pointer to the mesh.                  *
  62. *                                                                    *
  63. * Return Value : RCNOERROR                                           *
  64. *                RCWRITEDATA                                         *
  65. *                                                                    *
  66. * Comment      :                                                     *
  67. *                                                                    *
  68. \********************************************************************/
  69. ULONG write3RAWA(BPTR rawfile, TOCLMesh *mesh) {
  70.     UBYTE                         buffer[200];
  71.     TOCLPolygonNode            *pln=NULL;    
  72.     TOCLPolygonsVerticesNode    *plvi=NULL,*plv1=NULL,*plv2=NULL,*plv3=NULL;
  73.     TOCLVertex                    ver1,ver2,ver3;
  74.  
  75.     /*
  76.     ** Write the name of the mesh
  77.     */
  78.     if (FPrintf(rawfile,"%s\n",mesh->name)==ENDSTREAMCH) return(RCWRITEDATA);
  79.     
  80.     /*
  81.     ** Write the polygons as triangles, must be convex polygons !
  82.     */              
  83.       if(mesh->polygons.firstNode!=NULL) {                 
  84.         pln=mesh->polygons.firstNode;
  85.         do {
  86.             /* Get the first point of the polygon, used to create all triangles with it */
  87.             if(pln->numberOfVertices>=3) {
  88.                 plv1=pln->firstNode;
  89.                 ver1=plv1->vertexNode->vertex;
  90.             
  91.                 plvi=plv1;
  92.                 do {    
  93.                     plv2=plvi->next;
  94.                     plv3=plv2->next;
  95.                 
  96.                     ver2=plv2->vertexNode->vertex;
  97.                     ver3=plv3->vertexNode->vertex;
  98.                 
  99.                     sprintf(buffer,"%g %g %g %g %g %g %g %g %g\n",ver1.x,ver1.y,ver1.z,
  100.                     ver2.x,ver2.y,ver2.z,ver3.x,ver3.y,ver3.z);
  101.                     if(FPuts(rawfile,buffer)!=DOSFALSE) return(RCWRITEDATA);
  102.                     
  103.                     plvi=plvi->next;
  104.                 } while(plv3->next!=NULL);    
  105.             }
  106.         
  107.             /* Get the next polygon */
  108.             pln=pln->next;            
  109.         } while(pln!=NULL);
  110.     }
  111.     
  112.     return(RCNOERROR);
  113. }
  114.  
  115. /********************************************************************\
  116. *                                                                    *
  117. * Name         : write3RAWB                                          *
  118. *                                                                    *
  119. * Description  : Writes a standart RAW binary file.                  *
  120. *                                                                    *
  121. * Arguments    : rawfile IN  : An already opened file stream.        *
  122. *                mesh    IN  : Pointer to the mesh.                  *
  123. *                                                                    *
  124. * Return Value : RCNOERROR                                           *
  125. *                RCWRITEDATA                                         *
  126. *                                                                    *
  127. * Comment      :                                                     *
  128. *                                                                    *
  129. \********************************************************************/
  130. ULONG write3RAWB(BPTR rawfile, TOCLMesh *mesh) {
  131.     TOCLPolygonNode            *pln=NULL;    
  132.     TOCLPolygonsVerticesNode    *plvi=NULL,*plv1=NULL,*plv2=NULL,*plv3=NULL;
  133.     TOCLVertex                    ver1,ver2,ver3;
  134.     RAWTriangle                tbuffer[Ci_BUFFERS]; // Ci_BUFFERS * sizeof(RAWTriangle)    triangle buffer
  135.     ULONG                        bufferstate;
  136.     
  137.     /*
  138.     ** Write the name of the mesh, including its '\0' at the end
  139.     */
  140.     if(FWrite(rawfile,mesh->name,stringlen(mesh->name)+1,1)!=1) return(RCWRITEDATA);
  141.     
  142.     /*
  143.     ** Write the polygons as triangles, must be convex polygons !
  144.     */              
  145.       if(mesh->polygons.firstNode!=NULL) {                 
  146.         bufferstate=0;
  147.     
  148.         pln=mesh->polygons.firstNode;
  149.         do {
  150.             /* Get the first point of the polygon, used to create all triangles with it */
  151.             if(pln->numberOfVertices>=3) {
  152.                 plv1=pln->firstNode;
  153.                 ver1=plv1->vertexNode->vertex;
  154.             
  155.                 plvi=plv1;
  156.                 do {    
  157.                     plv2=plvi->next;
  158.                     plv3=plv2->next;
  159.                 
  160.                     ver2=plv2->vertexNode->vertex;
  161.                     ver3=plv3->vertexNode->vertex;
  162.                     
  163.                     tbuffer[bufferstate].x1=ver1.x;
  164.                     tbuffer[bufferstate].y1=ver1.y;
  165.                     tbuffer[bufferstate].z1=ver1.z;
  166.  
  167.                     tbuffer[bufferstate].x2=ver2.x;
  168.                     tbuffer[bufferstate].y2=ver2.y;
  169.                     tbuffer[bufferstate].z2=ver2.z;
  170.  
  171.                     tbuffer[bufferstate].x3=ver3.x;
  172.                     tbuffer[bufferstate].y3=ver3.y;
  173.                     tbuffer[bufferstate].z3=ver3.z;
  174.  
  175.                     // check if the buffer is full and write and initialize it
  176.                     if (++bufferstate==Ci_BUFFERS) {
  177.                         if(FWrite(rawfile,&tbuffer,Ci_BUFFERS*sizeof(RAWTriangle),1)!=1) return(RCWRITEDATA);
  178.                         bufferstate=0;
  179.                     }
  180.  
  181.                     plvi=plvi->next;
  182.                 } while(plv3->next!=NULL);    
  183.             }
  184.         
  185.             /* Get the next polygon */
  186.             pln=pln->next;            
  187.         } while(pln!=NULL);
  188.  
  189.         // check if the buffer has still some elements and write them
  190.         if (bufferstate>0) {
  191.             if(FWrite(rawfile,&tbuffer,bufferstate*sizeof(RAWTriangle),1)!=1) return(RCWRITEDATA);
  192.             bufferstate=0;
  193.         }
  194.     }
  195.     
  196.     return(RCNOERROR);
  197. }
  198.  
  199. /************************* End of file ******************************/
  200.